package br.com.portal.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import br.com.portal.beans.Representante;
import br.com.portal.factory.ConnectionFactory;

public class RepresentanteDAO {
	public static Representante getRepresentante(String usuario, String senha)
	{
		Representante rep = null;
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		String query = "SELECT C_BPARTNER_ID, USERNAME, EMAIL, AD_USER_ID, NAME, AD_ORG_ID, DEFAULT_AD_ORG_ID FROM AD_USER WHERE USERNAME = ? AND DESCRIPTION = ?";

			try {
				conn = ConnectionFactory.getConnection();
				stmt = conn.prepareStatement(query);
				stmt.setString(1,usuario);
				stmt.setString(2,senha);
				rs = stmt.executeQuery();
				if(rs.next()){
					rep = new Representante();
					rep.setBpid(rs.getString(1));
					rep.setCodigo(rs.getString(2));
					rep.setEmail(rs.getString(3));
					rep.setId(rs.getString(4));
					rep.setNome(rs.getString(5));
					rep.setOrg(OrgDAO.getOrg(rs.getString(6)));
					rep.setOrgPadrao(OrgDAO.getOrg(rs.getString(7)));
				}
				conn.close();
				stmt.close();
				rs.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		return rep;
	}
}
